package com.zlzlib.libui.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.zlzlib.libui.manager.AppManager;
import com.zlzlib.libui.util.ScreenUtil;


/**
 * Created by zlz on 2019/8/26.
 */
public abstract class BaseActivity<B extends ViewDataBinding> extends AppCompatActivity {

    protected B mBinding;

    /**
     * @return 布局id
     */
    protected abstract int getContentViewId();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * @return 是否处理回退事件
     */
    protected boolean backDo() {
        return false;
    }

    /**
     * @return 是否透明状态栏
     */
    protected boolean isTransStatus() {
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getInstance().addActivity(this);
        //去掉自带的ActionBar
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        initViewBand();
    }

    private void initViewBand() {
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), getContentViewId(), null, false);
        mBinding.getRoot().setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        setContentView(mBinding.getRoot());
        if (isTransStatus()) {
            ScreenUtil.setTranslucentStatus(this);
        }
        initView();
        initData();
    }

    protected void initView() {
    }

    /**
     * 使 android: windowSoftInputMode adjustResize   有效无效
     *
     * @param b true 有效   false 无效
     */
    protected void setTitleFitsSystem(boolean b) {
        mBinding.getRoot().setFitsSystemWindows(b);
    }

    @Override
    protected void onDestroy() {
        AppManager.getInstance().remove(this);
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {//按下返回键
            if (backDo()) {
                return false;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
